package org.kineticsproject.visualizations;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Dimension;

import java.util.Iterator;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JScrollPane;





public class SinglePlotChartMultipleAxis implements Chart
{

	private boolean	displayTable		 = false;
	private boolean displayPlotSelection = false;
	private JTable	table				 = null;
	
	public JPanelExt setupChart(List datasetList,String chartTitle, String tabTitle) 
	{
			JFreeChart 			xyChart;
			XYDataset 			curDataset;
			XYSeriesExt			curXYSeriesExt  = null;
			
			Iterator   			it 				= datasetList.iterator();
			XYPlot      		plot 			= null;
			int         		index 			= 1;
			ChartPanelExt 		xyChartPanel 	= null;
			GridBagConstraints 	c 				= null;
			JPanelExt           contentPanel    = null;
			JPanel              checkboxPanel   = null;
			JCheckBoxExt        checkbox		= null;
			JScrollPane			tablePane		= null;

			
		
			// find the first visible XYSeries
			boolean bSearching = true;
			
			while((bSearching) && (it.hasNext()))
			{
				curXYSeriesExt = (XYSeriesExt)it.next();
				
				if (curXYSeriesExt.isVisible()) // found first visible plot
				{
					bSearching = false;
				}
				else // no plot found yet, try again
				{
					curXYSeriesExt = null;
				}
			}
			
			
		if (curXYSeriesExt != null)	
		{
				curDataset = new XYSeriesCollection(curXYSeriesExt);
				
				checkboxPanel = new JPanel(new GridLayout(0,1,0,0));
				
				xyChart = ChartFactory.createTimeSeriesChart(chartTitle, curXYSeriesExt.getXLabel(), curXYSeriesExt.getYLabel()+" ("+curXYSeriesExt.getYUnits()+")",curDataset, true, true, true);
			    			
				contentPanel = new JPanelExt(new GridBagLayout());
				xyChartPanel = new ChartPanelExt(xyChart,chartTitle, tabTitle);
				

				
	

				xyChartPanel.setDomainZoomable(true);
				xyChartPanel.setRangeZoomable(true);
	            xyChartPanel.setBorder(BorderFactory.createCompoundBorder(
	            		BorderFactory.createEtchedBorder(),
	                    BorderFactory.createEmptyBorder(10, 10, 10, 10)
	                    ));
				
		        plot = xyChart.getXYPlot();
		        plot.setOrientation(PlotOrientation.VERTICAL);
		        plot.setBackgroundPaint(Color.white);
		        plot.setDomainGridlinePaint(Color.black);
		        plot.setRangeGridlinePaint(Color.black);
		        
		        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		        
		        plot.getRangeAxis().setFixedDimension(15.0);
		        XYItemRenderer renderer = plot.getRenderer();
		        
		        renderer.setPaint(curXYSeriesExt.getPlotColor());

		        NumberAxis axis = new NumberAxis(curXYSeriesExt.getYLabel()+" ("+curXYSeriesExt.getYUnits()+")");

		        axis.setFixedDimension(10.0);
		        axis.setAutoRangeIncludesZero(false);
		        axis.setLabelPaint(curXYSeriesExt.getlabelColor());
		        axis.setTickLabelPaint(curXYSeriesExt.getlabelColor());
		        
		        plot.setRangeAxis(0, axis);
		        plot.setRangeAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT);
		        
		      
				checkbox = new JCheckBoxExt(curXYSeriesExt.getYLabel(),true,renderer);
				checkbox.addActionListener(xyChartPanel);
				checkbox.setAxis(axis);
				checkboxPanel.add(checkbox);
				
		        
		        curXYSeriesExt.setIndex(0);

		        xyChartPanel.setCharType(ChartPanelExt.ChartType.SINGLEPLOT_MULTIPLEAXIS);
			
			while(it.hasNext())
			{
				curXYSeriesExt = (XYSeriesExt)it.next();
				
				if (curXYSeriesExt.isVisible()) // if visible display plot
				{
					
					
					curDataset = new XYSeriesCollection(curXYSeriesExt);				
					{
				        axis = new NumberAxis(curXYSeriesExt.getYLabel()+" ("+curXYSeriesExt.getYUnits()+")");
				        axis.setFixedDimension(10.0);
				        axis.setAutoRangeIncludesZero(false);
				        axis.setLabelPaint(curXYSeriesExt.getlabelColor());
				        axis.setTickLabelPaint(curXYSeriesExt.getlabelColor());
				   
				        plot.setRangeAxis(index, axis);
				        plot.setRangeAxisLocation(index, AxisLocation.BOTTOM_OR_LEFT);
				        plot.setDataset(index, curDataset);
				        plot.mapDatasetToRangeAxis(index,index);
				        renderer = new StandardXYItemRenderer();
				        renderer.setSeriesPaint(0, curXYSeriesExt.getPlotColor());
				        plot.setRenderer(index, renderer);
				        curXYSeriesExt.setIndex(index);
				   
				        
				        
				        checkbox = new JCheckBoxExt(curXYSeriesExt.getYLabel(),true,renderer);
						checkbox.addActionListener(xyChartPanel);
						checkbox.setAxis(axis);
						checkboxPanel.add(checkbox);
						
				        ++index;
					}
				}
			}

			
	}
			
			contentPanel.setTabTitle(xyChartPanel.getChartTitle());
			
            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 0;
            c.weightx = 1.0;
            c.weighty = 1.0;
            c.fill = GridBagConstraints.BOTH;
            c.anchor = GridBagConstraints.FIRST_LINE_START;
            c.insets = new Insets(12, 12, 0, 12);
            contentPanel.add(xyChartPanel, c);
          
            if (this.displayPlotSelection)
            {
            c = new GridBagConstraints();
            c.gridx = 1;
            c.gridy = 0;
            c.weightx = 0;
            c.weighty = 0;

           // c.fill = GridBagConstraints.BOTH;
           // c.anchor = GridBagConstraints.PAGE_END;
          //  c.insets = new Insets(12, 12, 0, 12);

            contentPanel.add(checkboxPanel, c);
            
            }
            if (this.displayTable)
            {
	            c = new GridBagConstraints();
	            c.gridx = 0;
	            c.gridy = 1;
	       //     c.weightx = 1.0;
	     //       c.weighty = 1.0;
	            c.fill = GridBagConstraints.HORIZONTAL;
	            c.anchor = GridBagConstraints.LAST_LINE_START;
	            c.insets = new Insets(12, 12, 12, 12);
	            tablePane = new JScrollPane(table);
	           // Dimension d = table.getPreferredSize();
	           // d.setSize(d.getWidth(), d.getHeight()+20);
	            table.setPreferredScrollableViewportSize(table.getPreferredSize());
	           // tablePane.setPreferredSize(d);
	            contentPanel.add(tablePane, c);
            }
            
            
			return(contentPanel);
	}

	public boolean isDisplayTable()
	{
		return displayTable;
	}

	public void setDisplayTable(boolean displayTable)
	{
		this.displayTable = displayTable;
	}

	public JTable getTable()
	{
		return table;
	}

	public void setTable(JTable table)
	{
		this.table = table;
	}

	public boolean isDisplayPlotSelection()
	{
		return displayPlotSelection;
	}

	public void setDisplayPlotSelection(boolean displayPlotSelection)
	{
		this.displayPlotSelection = displayPlotSelection;
	}
	



}
